package com.hao.eduorder.controller;


import com.hao.commonutils.JwtUtils;
import com.hao.commonutils.R;
import com.hao.eduorder.entity.Order;
import com.hao.eduorder.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author hao
 * @since 2021-01-20
 */
@RestController
@CrossOrigin
@RequestMapping("/eduorder/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 创建订单
     *
     * @param courseId 进程id
     * @param request  请求
     * @return {@link R}
     */
    @PostMapping("createOrder/{courseId}")
    public R createOrder(@PathVariable String courseId, HttpServletRequest request) {
        //创建订单，得到订单号
        String userId = JwtUtils.getMemberIdByJwtToken(request);
        String orderNo = orderService.createOrders(courseId, userId);
        return R.ok().data("orderNo", orderNo);
    }

    /**
     * 获取订单信息
     *
     * @param orderNo 订单没有
     * @return {@link R}
     */
    @GetMapping("getOrderInfo/{orderNo}")
    public R getOrderInfo(@PathVariable String orderNo) {
        Order order = orderService.getByOrderNo(orderNo);
        return R.ok().data("order",order);
    }

    /**
     * 是否已经购买课程
     *
     * @param courseId 课程id
     * @param memberId 学员id
     * @return boolean
     */
    @GetMapping("isBuyCourse/{courseId}/{memberId}")
    public boolean isBuyCourse(@PathVariable String courseId,@PathVariable String memberId){
        return orderService.isBuyCourse(courseId,memberId);
    }

}

